(function(ns, win, doc, cfgs){

ns.addPlay(1, {
    Extends: ns.Play,
    options: {
        length: 1
	},
    build: function(ops, self) {
        self.balls = [new ns.Balls({min: 1, max: 11, mid: 5})];
        self.element = self.balls[0].container;
        self.suffix = ops.length > 1 ? '' : new Array(5).join('|_');
        ops.sum = ops.length;
    },
    compute: function (balls) {
        var self = this,
            ops = self.options,
            len = balls.length,
            info = {nums: len, count: 0};
        info.balls = balls.join(',') + self.suffix;
        if (len >= ops.length) {
            info.count = len.combination(ops.length);
        }
        return info;
    },
    change: function () {
        var self = this,
            balls = self.balls[0].get();
            self.info = self.compute(balls);
        self.parent();
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            while (n -- > 0) {
                nums = self.balls[0].random(ops.length).join(',');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums + self.suffix}));
            }
            return html.join('');
        } else {
            balls = self.balls[0].get();
            info = self.compute(balls);
            return balls.length >= ops.length ? tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: info.balls, balls: info.balls}) : '';            
        }
    }
});

ns.addPlay(2, {
    Extends: ns.getPlay(1),
    options: {
        length: 2
    }
});

ns.addPlay(3, {
    Extends: ns.getPlay(1),
    options: {
        length: 3
    }
});

ns.addPlay(4, {
    Extends: ns.getPlay(1),
    options: {
        length: 4
    }
});

ns.addPlay(5, {
    Extends: ns.getPlay(1),
    options: {
        length: 5
    }
});

ns.addPlay(6, {
    Extends: ns.getPlay(1),
    options: {
        length: 6
    }
});

ns.addPlay(7, {
    Extends: ns.getPlay(1),
    options: {
        length: 7
    }
});

ns.addPlay(8, {
    Extends: ns.getPlay(1),
    options: {
        length: 8
    }
});

ns.addPlay(24, {
    Extends: ns.Play,
    build: function(ops, self) {
        self.element = new Element('div');
        self.balls = [new ns.Balls({title: '第一个', min: 1, max: 11, mid: 5}), new ns.Balls({title: '第二个', min: 1, max: 11, mid: 5})];
        self.suffix = new Array(ops.bytes - 1).join('|-');
        self.balls.invoke('inject', self.element);
    },
    compute: function (balls) {
        var self = this,
            b0Len = balls[0].length,
            b1Len = balls[1].length,
            info = {nums: b0Len + b1Len, count: 0},
            result = balls[0].slice(0),
            rLen;
        info.balls = balls.invoke('join', ',').join('|');
        if (b0Len && b1Len) {
            result.combine(balls[1]);
            rLen = result.length
            info.count = b0Len * b1Len - (info.nums - rLen);
        }
        return info;
    },
    change: function () {
        var self = this,
            balls = self.balls.invoke('get');
            self.info = self.compute(balls);
        self.parent();
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            balls,nums,info,html;
        if (n) {
            html = [];
            while (n -- > 0) {
                nums = self.balls[0].random(2).join('|');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums + suffix}));
            }
            return html.join('');
        } else {
            balls = self.balls.invoke('get');
            info = self.compute(balls);
            return balls[0].length && balls[1].length ? tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: info.balls, balls: info.balls + suffix}) : '';            
        }
    }
});

ns.addPlay(29, {
    Extends: ns.getPlay(2)
});

ns.addPlay(31, {
    Extends: ns.CombPlay,
    options: {
        sum: 3,
        randomType: 29
    },
    build: function(ops, self) {
        self.element = new Element('div');
        self.balls = [new ns.BileBalls({title: '胆码', min: 1, max: 11, mid: 5, lower:1, upper:ops.sum - 2}), new ns.BileBalls({title: '拖码', min: 1, max: 11, mid: 5, lower:2, upper:10})];
        self.suffix = new Array(ops.bytes - 1).join('|-');
        self.balls.invoke('inject', self.element);
    },

    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            while (n -- > 0) {
                nums = self.balls[0].random(ops.sum - 1).join(',');//机选时为前N组选
                html.push(tpl({cost: ops.cost, type: ops.randomType/*ops.type*/, name: cfgs.plays[ops.randomType].name/*ops.name*/, count: 1, nums: nums, balls: nums}));
            }
            return html.join('');
        } else {
            if (!self.validate()) {
                App.Alert('胆码加拖码至于要有' + ops.sum + '个');
                return '';
            }
            if (self.balls.invoke('check').every(function(r){return r;})) {
                balls = self.balls.invoke('get');
                info = self.compute(balls);
                return tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: balls.invoke('join', ',').join('|'), balls: info.balls});                          
            }
        }
    }
});

ns.addPlay(17, {
    Extends: ns.getPlay(31)
});

ns.addPlay(34, {
    Extends: ns.Play,
    build: function(ops, self) {
        self.element = new Element('div');
        self.balls = [new ns.Balls({title: '第一个', min: 1, max: 11, mid: 5}), new ns.Balls({title: '第二个', min: 1, max: 11, mid: 5}), new ns.Balls({title: '第三个', min: 1, max: 11, mid: 5})];
        self.suffix = new Array(ops.bytes - 2).join('|-');
        self.balls.invoke('inject', self.element);
    },
    compute: function (balls) {
        var self = this,
            lens = balls.invoke('count'),
            info = {nums: eval(lens.join('+')), count: 0},
            result = balls[0].slice(0),
            rLen;
        info.balls = balls.invoke('join', ',').join('|');
        info.count = balls[0].lottery(balls[1], balls[2]);
        return info;
    },
    change: function () {
        var self = this,
            balls = self.balls.invoke('get');
            self.info = self.compute(balls);
        self.parent();
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            balls,nums,info,html;
        if (n) {
            html = [];
            while (n -- > 0) {
                nums = self.balls[0].random(3).join('|');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums + suffix}));
            }
            return html.join('');
        } else {
            balls = self.balls.invoke('get');
            info = self.compute(balls);
            return balls[0].length && balls[1].length && balls[2].length ? tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: info.balls, balls: info.balls + suffix}) : '';            
        }
    }
});

ns.addPlay(39, {
    Extends: ns.Play,
    build: function(ops, self) {
        self.balls = [new ns.Balls({min: 1, max: 11, mid: 5})];
        self.element = self.balls[0].container;
    },
    compute: function (balls) {
        var self = this,
            len = balls.length,
            info = {nums: len, count: 0};
        info.balls = balls.join(',');
        if (len > 2) {
            info.count = len.combination(3);
        }
        return info;
    },
    change: function () {
        var self = this,
            balls = self.balls[0].get();
            self.info = self.compute(balls);
        self.parent();
    },
    get: function (n, tpl) {
        var self = this,
            ops = self.options,
            suffix = self.suffix,
            balls,
            nums,
            info,
            html;
        if (n) {
            html = [];
            while (n -- > 0) {
                nums = self.balls[0].random(3).join(',');
                html.push(tpl({cost: ops.cost, type: ops.type, name: ops.name, count: 1, nums: nums, balls: nums}));
            }
            return html.join('');
        } else {
            balls = self.balls[0].get();
            info = self.compute(balls);
            return balls.length > 2 ? tpl({cost: ops.cost, type: ops.type, name: ops.name, count: info.count, nums: info.balls, balls: info.balls}) : '';            
        }
    }
});

ns.addPlay(41, {
    Extends: ns.getPlay(31),
    options: {
        sum: 4,
        randomType: 39
    }
});

ns.addPlay(18, {
    Extends: ns.getPlay(41)
});

ns.addPlay(19, {
    Extends: ns.getPlay(31),
    options: {
        sum: 5,
        randomType: 4
    }
});

ns.addPlay(20, {
    Extends: ns.getPlay(31),
    options: {
        sum: 6,
        randomType: 5
    }
});

ns.addPlay(21, {
    Extends: ns.getPlay(31),
    options: {
        sum: 7,
        randomType: 6
    }
});

ns.addPlay(22, {
    Extends: ns.getPlay(31),
    options: {
        sum: 8,
        randomType: 7
    }
});

ns.addPlay(23, {
    Extends: ns.getPlay(31),
    options: {
        sum: 9,
        randomType: 8
    }
});

new ns.Lottery(cfgs.lottery);
})(using('HB'), this, document, this.configs || {});
